#define _CRT_SECURE_NO_WARNINGS
#include "Queue.h"

void QueueInit(Queue* qu)
{
	assert(qu);
	qu->phead = qu->ptail = NULL;
	qu->size = 0;
}

void QueueDesroy(Queue* qu)
{
	assert(qu);
	QNode* pcur = qu->phead;
	while (pcur)
	{
		QNode* next = pcur->next;
		free(pcur);
		pcur = next;
	}
	qu->phead = qu->ptail = NULL;
	qu->size = 0;
}

void QueuePush(Queue* qu, QDataType x)
{
	assert(qu);
	QNode* newnode = (QNode*)malloc(sizeof(QNode));
	if (newnode == NULL)
	{
		perror("malloc fail!");
		return;
	}
	newnode->val = x;
	newnode->next = NULL;
	if (qu->ptail == NULL)
	{
		qu->phead = qu->ptail = newnode;
	}
	else
	{
		qu->ptail->next = newnode;
		qu->ptail = newnode;
	}
	qu->size--;
}

void QueuePop(Queue* qu)
{
	assert(qu);
	assert(qu->size != 0);
	if (qu->phead->next == NULL)
	{
		free(qu->phead);
		qu->phead = qu->ptail = NULL;
	}
	else 
	{
		QNode* pcur = qu->phead;
		qu->phead = qu->phead->next;
		free(pcur);
	}
	qu->size--;
}

QDataType QueueFront(Queue* qu)
{
	assert(qu);
	assert(qu->phead);
	return qu->phead->val;
}

QDataType QueueBack(Queue* qu)
{
	assert(qu);
	assert(qu->ptail);
	return qu->ptail->val;
}

int QueueSize(Queue* qu)
{
	assert(qu);
	return qu->size;
}
bool QueueEmpty(Queue* qu)
{
	assert(qu);
	return qu->size == 0;
}
